package com.gitee.clarence.business.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.gitee.clarence.business.user.model.User;
import com.gitee.clarence.business.user.repository.UserRepository;
import com.gitee.clarence.business.user.ro.UserLoginRO;
import com.gitee.clarence.business.user.ro.UserRegisterRO;
import com.gitee.clarence.business.user.service.UserService;
import com.gitee.clarence.common.enums.ResultEnum;
import com.gitee.clarence.interceptor.exception.ServiceException;
import com.gitee.clarence.util.BCrypt;
import com.gitee.clarence.util.JWTUtil;

import lombok.extern.slf4j.Slf4j;

@Service
@Transactional
@Slf4j
public class UserServiceImpl  implements UserService{

	  @Autowired
	    private UserRepository userRepository;

	    @Override
	    public Boolean adminUserLogin(UserLoginRO userLoginRO) {
	        User user = userRepository.findByUsernameAndPasswordAndRole(userLoginRO.getUsername(), userLoginRO.getPassword(), 0);
	        if (user == null) {
	            throw new ServiceException(ResultEnum.ERROR_LOGIN);
	        }
	        return true;
	    }

	    @Override
	    public Map<String, Object> normalUserLogin(UserLoginRO userLoginRO) {
	        User user = userRepository.findByUsernameAndRole(userLoginRO.getUsername(), 1);
	        if (user == null) {
	            throw new ServiceException(ResultEnum.ERROR_LOGIN);
	        }
	        String password = userLoginRO.getPassword();
	        String hashPassword = user.getPassword();
	        String salt = hashPassword.substring(0, 29);
	        String hashed = BCrypt.hashpw(password, salt);
	        Map<String, Object> resMap = new HashMap<>();
	        if (hashed.equals(hashPassword)) {
	            Map<String, Object> map = new HashMap<>();
	            map.put("id", user.getId());
	            String token = null;
	            try {
	                token = JWTUtil.createJWT(JSON.toJSONString(map));
	            } catch (Exception e) {
	                log.error("【JWT加密】JWT加密过程中出现错误,subject:{}", JSON.toJSONString(map));
	                e.printStackTrace();
	            }
	            resMap.put("token", token);
	            resMap.put("id", user.getId());
	        } else {
	            resMap.put("error", ResultEnum.AUTH_ERROR);
	        }
	        return resMap;
	    }

	    @Override
	    public User normalUserRegister(UserRegisterRO userRegisterRO) {
	        User user = new User();
	        BeanUtils.copyProperties(userRegisterRO, user);
	        String salt = BCrypt.gensalt(); //生成29位salt字符串
	        String hashed = BCrypt.hashpw(userRegisterRO.getPassword(), salt);
	        user.setPassword(hashed);
	        return userRepository.save(user);
	    }

		@Override
		public List<User> findAll() {
			return userRepository.findAll();
		}

		@Override
		public User findById(Integer userId) {
			
			return userRepository.getOne(userId);
		}
}
